package com.hzdh.account.commom.validate;

import java.io.Serializable;

/**
 * Ajax返回信息，由前端ExceptionHandler处理，配置前端Ajax请求使用。 
 * 用于自动向用户展示Ajax请求异常信息。
 * @author wumingfei
 *
 * @param <T>
 */
public class AjaxResponse<T> implements Serializable {

	/** 请求状态栏 */
	private Boolean success;

	/** 信息代码 */
	private String code;

	/** 请求状态描述 */
	private String message;

	/** 请求数据内容 */
	private T content;

	/** 请求异常信息 */
	private Object exception;

	public AjaxResponse() {
		this(Boolean.TRUE, "操作成功");
	}

	public AjaxResponse(Boolean success) {
		this(success, null);
	}

	public AjaxResponse(String message) {
		this(Boolean.TRUE, "操作成功");
	}

	public AjaxResponse(Boolean success, String message) {
		this.success = success;
		this.message = message;
		if (this.message == null) {
			if (Boolean.FALSE.equals(success)) {
				this.message = "操作失败";
			}
			if (Boolean.TRUE.equals(success)) {
				this.message = "操作成功";
			}

		}
	}

	public AjaxResponse(Boolean success, String message, T content) {
		this.success = success;
		this.message = message;
		this.content = content;
		if (this.message == null) {
			if (Boolean.FALSE.equals(success)) {
				this.message = "操作失败";
			}
			if (Boolean.TRUE.equals(success)) {
				this.message = "操作成功";
			}

		}
	}

	public static AjaxResponse fail() {
		return fail(null);
	}

	public static AjaxResponse fail(String message) {
		return new AjaxResponse(Boolean.FALSE, message);
	}

	public static AjaxResponse success() {
		return success(null);
	}

	public static AjaxResponse success(String message) {
		return new AjaxResponse(Boolean.TRUE, message);
	}

	public static AjaxResponse successContent(Object content) {
		AjaxResponse res = AjaxResponse.success();
		res.setContent(content);
		return res;
	}

	public static AjaxResponse failException(Object exception) {
		AjaxResponse res = AjaxResponse.fail();
		res.setException(exception);
		return res;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getContent() {
		return content;
	}

	public void setContent(T content) {
		this.content = content;
	}

	public Object getException() {
		return exception;
	}

	public void setException(Object exception) {
		this.exception = exception;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
